package com.zhulong.saas.cloud.gateway.oauth2;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;

import java.util.Collection;

public class BearerTokenAuthenticationToken extends AbstractAuthenticationToken {

    private String token;

    public BearerTokenAuthenticationToken(String token) {
        super(null);
        Assert.notNull(token, "token can not be null");
        this.token = token;
    }

    public BearerTokenAuthenticationToken(String token, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        Assert.notNull(token, "token can not be null");
        this.token = token;
        super.setAuthenticated(true);
    }

    public String getToken() {
        return token;
    }

    @Override
    public void setAuthenticated(boolean authenticated) {
        if (authenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        } else {
            super.setAuthenticated(false);
        }
    }

    @Override
    public Object getCredentials() {
        return this.token;
    }

    @Override
    public Object getPrincipal() {
        return this.getToken();
    }
}
